//
//  SCClipImageView.m
//  lg_zhinengbeikao_english
//
//  Created by Chao Sheng on 2021/12/13.
//  Copyright © 2021 zhukai. All rights reserved.
//

#import "SCClipImageView.h"

@interface SCClipImageView ()

@property (nonatomic, strong) UIButton *leftTopBtn;
@property (nonatomic, strong) UIButton *midTopBtn;
@property (nonatomic, strong) UIButton *rightTopBtn;

@property (nonatomic, strong) UIButton *leftMidBtn;
@property (nonatomic, strong) UIButton *rightMidBtn;

@property (nonatomic, strong) UIButton *leftBottomBtn;
@property (nonatomic, strong) UIButton *midBottomBtn;
@property (nonatomic, strong) UIButton *rightBottomBtn;

@property (nonatomic, strong) NSMutableArray *layerArray;

@property (nonatomic, assign) CGRect preFrame;
@property (nonatomic, assign) CGRect oldFrame;
@property (nonatomic, assign) CGFloat maxW;
@property (nonatomic, assign) CGFloat maxH;

@end

@implementation SCClipImageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.preFrame = frame;
        self.oldFrame = frame;
        self.layerArray = @[].mutableCopy;
        self.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.1];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.leftTopBtn];
    [self addSubview:self.leftMidBtn];
    [self addSubview:self.leftBottomBtn];
    
    [self addSubview:self.midTopBtn];
    [self addSubview:self.midBottomBtn];
    
    [self addSubview:self.rightTopBtn];
    [self addSubview:self.rightMidBtn];
    [self addSubview:self.rightBottomBtn];
    
    [self.leftTopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.width.height.equalTo(@40);
    }];
    [self.leftMidBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.centerY.equalTo(self);
        make.width.height.equalTo(@40);
    }];
    [self.leftBottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.width.height.equalTo(@40);
    }];

    [self.rightTopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(@0);
        make.width.height.equalTo(@40);
    }];
    [self.rightMidBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0);
        make.centerY.equalTo(self);
        make.width.height.equalTo(@40);
    }];
    [self.rightBottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.width.height.equalTo(@40);
    }];

    [self.midTopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.centerX.equalTo(self);
        make.width.height.equalTo(@40);
    }];
    [self.midBottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@0);
        make.centerX.equalTo(self);
        make.width.height.equalTo(@40);
    }];
}

- (void)resetView:(CGRect)frame {
    self.frame = frame;
    self.preFrame = frame;
    self.oldFrame = frame;

    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self setupUI];
}

- (void)layoutSubviews {
    [super layoutSubviews];
   
    if (self.layerArray.count>0) {
        for (CALayer *layer in self.layerArray ) {
            [layer removeFromSuperlayer];
        }
    }
   
    CGFloat h = self.frame.size.height;
    CGFloat w = self.frame.size.width;
    
    NSArray *borders1 = @[@(CGPointMake(0, 0)),@(CGPointMake(w, 0)),@(CGPointMake(w, h)),@(CGPointMake(0, h)),@(CGPointMake(0, 0))];
    [self drawBoderLine:borders1];
    NSArray *borders2 = @[@(CGPointMake(w/3, 0)),@(CGPointMake(w/3, h))];
    [self drawBoderLine:borders2];
    NSArray *borders3 = @[@(CGPointMake(2*w/3, 0)),@(CGPointMake(2*w/3, h))];
    [self drawBoderLine:borders3];
    NSArray *borders4 = @[@(CGPointMake(0, h/3)),@(CGPointMake(w, h/3))];
    [self drawBoderLine:borders4];
    NSArray *borders5 = @[@(CGPointMake(0, 2*h/3)),@(CGPointMake(w, 2*h/3))];
    [self drawBoderLine:borders5];
    
    NSArray *leftTops = @[@(CGPointMake(0, 20)),@(CGPointMake(0, 0)),@(CGPointMake(20, 0))];
    [self drawLine:leftTops];
    
    NSArray *leftMids = @[@(CGPointMake(0,h/2-12.5)),@(CGPointMake(0,h/2+12.5))];
    [self drawLine:leftMids];
    
    NSArray *leftBottom = @[@(CGPointMake(0,h-20)),@(CGPointMake(0,h)),@(CGPointMake(20, h))];
    [self drawLine:leftBottom];
    
    NSArray *rightTops = @[@(CGPointMake(w-20,0)),@(CGPointMake(w, 0)),@(CGPointMake(w, 20))];
    [self drawLine:rightTops];

    NSArray *rightMids = @[@(CGPointMake(w,h/2-12.5)),@(CGPointMake(w,h/2+12.5))];
    [self drawLine:rightMids];

    NSArray *rightBottom = @[@(CGPointMake(w,h-20)),@(CGPointMake(w,h)),@(CGPointMake(w-20, h))];
    [self drawLine:rightBottom];

    NSArray *topMids = @[@(CGPointMake(w/2-12.5,0)),@(CGPointMake(w/2+12.5,0))];
    [self drawLine:topMids];

    NSArray *bottomMids = @[@(CGPointMake(w/2-12.5,h)),@(CGPointMake(w/2+12.5,h))];
    [self drawLine:bottomMids];

}

- (void)drawLine:(NSArray*)points {
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPath];
    for (NSInteger i = 0; i < points.count; i++) {
        CGPoint point = ((NSValue*)points[i]).CGPointValue;
        if (i==0) {
            [path moveToPoint:point];
        } else {
            [path addLineToPoint:point];
        }
    }
    
    layer.path = path.CGPath;
    layer.lineWidth = 2.0f;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor colorWithRed:0 green:153.0/255 blue:255.0/255 alpha:1].CGColor;
    layer.frame = self.bounds;
    [self.layer addSublayer:layer];
    [self.layerArray addObject:layer];
}

- (void)drawBoderLine:(NSArray*)points {
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPath];
    for (NSInteger i = 0; i < points.count; i++) {
        CGPoint point = ((NSValue*)points[i]).CGPointValue;
        if (i==0) {
            [path moveToPoint:point];
        } else {
            [path addLineToPoint:point];
        }
    }
    
    layer.path = path.CGPath;
    layer.lineWidth = 1.0f;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor whiteColor].CGColor;
    layer.frame = self.bounds;
    [self.layer addSublayer:layer];
    [self.layerArray addObject:layer];
}

#pragma mark - 拖动
- (void)pan:(UIPanGestureRecognizer*)sender {
    
    CGFloat h = self.preFrame.size.height;
    CGFloat w = self.preFrame.size.width;
    CGFloat x = self.preFrame.origin.x;
    CGFloat y = self.preFrame.origin.y;
    
    UIView *view = sender.view;
    //translationInView 获取到的是手指移动后，在相对坐标中的偏移量
    CGPoint point = [sender translationInView:view];
    CGRect rect;

    if (view==self.leftTopBtn) {
        rect = CGRectMake(x+point.x,y+point.y,w-point.x, h-point.y);
    } else if(view==self.leftMidBtn) {
        rect = CGRectMake(x+point.x,y,w-point.x,h);
    } else if(view==self.leftBottomBtn) {
        rect = CGRectMake(x+point.x,y,w-point.x,h+point.y);
    } else if (view==self.midTopBtn) {
        rect = CGRectMake(x,y+point.y,w, h-point.y);
    } else if (view==self.midBottomBtn) {
        rect = CGRectMake(x,y,w, h+point.y);
    } else if (view==self.rightTopBtn) {
        rect = CGRectMake(x,y+point.y,w+point.x, h-point.y);
    } else if (view==self.rightMidBtn) {
        rect = CGRectMake(x,y,w+point.x,h);
    } else {
        rect = CGRectMake(x,y,w+point.x,h+point.y);
    }
    
    rect = [self limitMaxAndMinXYWH:rect];
    
//    if (rect.size.width >= self.oldFrame.size.width) {
//        rect.size.width = self.oldFrame.size.width;
//        rect.origin.x = self.oldFrame.origin.x;
//    }
//
//    if (rect.size.height >= self.oldFrame.size.height) {
//        rect.size.height = self.oldFrame.size.height;
//        rect.origin.y = self.oldFrame.origin.y;
//    }
//
//    if (rect.size.width < self.oldFrame.size.width/3) {
//        rect.origin.x = self.frame.origin.x;
//        rect.origin.y = self.frame.origin.y;
//        rect.size.width = self.oldFrame.size.width/3;
//        rect.size.height = self.frame.size.height;
//    }
//
//    if (rect.size.height < self.oldFrame.size.height/3) {
//        rect.origin.x = self.frame.origin.x;
//        rect.origin.y = self.frame.origin.y;
//        rect.size.width = self.frame.size.width;
//        rect.size.height = self.oldFrame.size.height/3;
//    }
    
    self.frame = rect;
    if (sender.state==UIGestureRecognizerStateEnded) {
        self.preFrame = self.frame;
    }

}

//限制最大和最小宽高和xy坐标
- (CGRect)limitMaxAndMinXYWH:(CGRect)rect {
    //最大宽
    if (rect.size.width >= self.oldFrame.size.width) {
        rect.size.width = self.oldFrame.size.width;
        rect.origin.x = self.oldFrame.origin.x;
    }
    //最大高
    if (rect.size.height >= self.oldFrame.size.height) {
        rect.size.height = self.oldFrame.size.height;
        rect.origin.y = self.oldFrame.origin.y;
    }
    
    //最小宽
    if (rect.size.width < self.oldFrame.size.width/3) {
        rect.origin.x = self.frame.origin.x;
        rect.origin.y = self.frame.origin.y;
        rect.size.width = self.oldFrame.size.width/3;
        rect.size.height = self.frame.size.height;
    }
    //最小高
    if (rect.size.height < self.oldFrame.size.height/3) {
        rect.origin.x = self.frame.origin.x;
        rect.origin.y = self.frame.origin.y;
        rect.size.width = self.frame.size.width;
        rect.size.height = self.oldFrame.size.height/3;
    }
    //最大x
    if (rect.origin.x+rect.size.width >= self.oldFrame.size.width+self.oldFrame.origin.x) {
        rect.size.width = self.oldFrame.size.width+self.oldFrame.origin.x-rect.origin.x;
    }
    //最大y
    if (rect.origin.y+rect.size.height >= self.oldFrame.size.height+self.oldFrame.origin.y) {
        rect.size.height = self.oldFrame.size.height+self.oldFrame.origin.y-rect.origin.y;
    }
    //最小x
    if (rect.origin.x <= self.oldFrame.origin.x) {
        rect.size.width = rect.size.width- (self.oldFrame.origin.x - rect.origin.x);
        rect.origin.x = self.oldFrame.origin.x;
    }
    //最小y
    if (rect.origin.y <= self.oldFrame.origin.y) {
        rect.size.height = rect.size.height - (self.oldFrame.origin.y - rect.origin.y);
        rect.origin.y = self.oldFrame.origin.y;
    }
    
    return rect;
}

#pragma mark - 懒加载
- (UIButton *)leftTopBtn {
    if (!_leftTopBtn) {
        _leftTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_leftTopBtn addGestureRecognizer:pan];
        //_leftTopBtn.backgroundColor = [UIColor redColor];
    }
    return _leftTopBtn;
}

- (UIButton *)leftMidBtn {
    if (!_leftMidBtn) {
        _leftMidBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_leftMidBtn addGestureRecognizer:pan];
        //_leftMidBtn.backgroundColor = [UIColor redColor];
    }
    return _leftMidBtn;
}

- (UIButton *)leftBottomBtn {
    if (!_leftBottomBtn) {
        _leftBottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_leftBottomBtn addGestureRecognizer:pan];
        //_leftBottomBtn.backgroundColor = [UIColor redColor];
    }
    return _leftBottomBtn;
}

- (UIButton *)midTopBtn {
    if (!_midTopBtn) {
        _midTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_midTopBtn addGestureRecognizer:pan];
        //_midTopBtn.backgroundColor = [UIColor redColor];
        
    }
    return _midTopBtn;
}

- (UIButton *)midBottomBtn {
    if (!_midBottomBtn) {
        _midBottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_midBottomBtn addGestureRecognizer:pan];
        //_midBottomBtn.backgroundColor = [UIColor redColor];
    }
    return _midBottomBtn;
}

- (UIButton *)rightTopBtn {
    if (!_rightTopBtn) {
        _rightTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_rightTopBtn addGestureRecognizer:pan];
        //_rightTopBtn.backgroundColor = [UIColor redColor];
    }
    return _rightTopBtn;
}

- (UIButton *)rightMidBtn {
    if (!_rightMidBtn) {
        _rightMidBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_rightMidBtn addGestureRecognizer:pan];
        //_rightMidBtn.backgroundColor = [UIColor redColor];
    }
    return _rightMidBtn;
}

- (UIButton *)rightBottomBtn {
    if (!_rightBottomBtn) {
        _rightBottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_rightBottomBtn addGestureRecognizer:pan];
        //_rightBottomBtn.backgroundColor = [UIColor redColor];
    }
    return _rightBottomBtn;
}

@end
